package com.zsrd.system.controller;


import com.zsrd.auth.core.UserInfoUtil;
import com.zsrd.core.response.ResponseSupport;
import com.zsrd.system.domain.SysDept;
import com.zsrd.system.service.SysDeptService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Xxx
 * @since 2023-11-23
 */
@RestController
@RequestMapping("/sysDept")
@Api(value="部门列表",tags="部门列表")
public class SysDeptController {

    @Autowired
    private SysDeptService sysDeptService;


    /**
     * 根据条件查询树结构
     * 如果参数为空就查全部
     * 如果参数有值就根据参数去查寻
     * @param
     * @return
     */
    @PostMapping(value = "/list")
    public ResponseSupport list(@RequestBody SysDept sysDept) {
        List<SysDept> list = sysDeptService.listByNameOrStatus(sysDept);
        return ResponseSupport.success(list);
    }


    /**
     * 删除
     * @return
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ApiOperation(value = "删除",notes = "删除")
    public ResponseSupport delete(@RequestBody SysDept sysDept){
        sysDept.setDelFlag(2);
        List<String> list = Arrays.asList("admin", "manager");
        List<String> roleList = UserInfoUtil.getUserRole();
        if (roleList.containsAll(list)){
            sysDeptService.removeById(sysDept.getDeptId());
            return  ResponseSupport.success("删除成功");
        }
        sysDept.setUpdateTime(new Date());
        boolean b = sysDeptService.updateById(sysDept);

        if (b==false){
            return ResponseSupport.fail("删除失败");
        }

        return ResponseSupport.success(b);
    }



    @RequestMapping(value = "save",method = RequestMethod.POST)
    @ApiOperation(value = "新增",notes = "新增")
    public ResponseSupport saveOrUpdate(@RequestBody SysDept sysDept){
             sysDept.setDelFlag(0);
             if (sysDept.getDeptId()==null) {
                 if (sysDept.getParentDeptId() == 0L) {
                     sysDept.setCreateTime(new Date());
                     sysDept.setUpdateTime(new Date());
                     sysDept.setParentDeptId(0L);
                     sysDeptService.save(sysDept);
                 } else {
                     sysDept.setCreateTime(new Date());
                     sysDept.setUpdateTime(new Date());
                     sysDept.setParentDeptId(sysDept.getParentDeptId());
                     sysDeptService.save(sysDept);
                 }
             }else {
                 sysDept.setUpdateTime(new Date());
                 sysDept.setCreateTime(new Date());
                 boolean b = sysDeptService.saveOrUpdate(sysDept);
                 if (b==false){
                     return ResponseSupport.fail("修改失败");
                 }
                 return ResponseSupport.success(b);
             }
        return ResponseSupport.success();
    }







}

